﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class Manager_PictureLibraryAdd : System.Web.UI.PageEx
{

    private string strPageCode = "Q0000B";
    private string strLoginResult = string.Empty;
    private int iSelectId = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        int.TryParse(Convert.ToString(Request.QueryString["PictureId"]), out iSelectId);
        if (!DalEx.DbManagerEx.CheckLoginRight(UserType, UserId, strPageCode, ref strLoginResult))
        {
            FineUI.Alert.Show(strLoginResult, FineUI.MessageBoxIcon.Warning);
            Response.Redirect("Login.aspx");
        }
        if (!Page.IsPostBack)
        {
            DataBinds();
            btnReset.OnClientClick = sfObject.GetResetReference();
        }
    }
    private void DataBinds()
    {
        if (iSelectId > 0)//如果产品ID>0时绑定产品属性
        {
            Model.DbPicture dbPicture = DalEx.DbPictureEx.Select(iSelectId);
            if (dbPicture != null)
            {
                fuLogo.Required = false;
                hfPictureId.Text = dbPicture.PId.ToString();
                txtTitle.Text = dbPicture.PTitle;
                cboxStatus.Checked = Convert.ToBoolean(dbPicture.PStatus);
                UEditor1.Value = HttpUtility.HtmlDecode(dbPicture.PNotes);
                txtHits.Text = Convert.ToString(dbPicture.PViews);
                txtOrder.Text = Convert.ToString(dbPicture.POrder);
            }
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string strErrorMsg = string.Empty;
        Int16 iHits = 0, iOrder = 0, iStatus = 0;
        int iSelectId = 0, iSelectClassId=0;
        string strDefaultPicture = string.Empty;
        string strTitle = txtTitle.Text.Trim();
        string strHtmlTitle = string.Empty;
        string strHits = txtHits.Text.Trim();
        string strOrder = txtOrder.Text.Trim();
        string strSelectId = hfPictureId.Text.Trim();
        string strNotes = HttpUtility.HtmlEncode(UEditor1.Value);
        Int32.TryParse(strSelectId, out iSelectId);
        Int16.TryParse(strOrder, out iOrder);
        Int16.TryParse(strHits, out iHits);
        iStatus = Convert.ToInt16(cboxStatus.Checked);

        strDefaultPicture = WebBase.Comm.GetFilePath(fuLogo, Server.MapPath("~/upload/PictureLibrary/"), ResolveUrl("~/upload/PictureLibrary/"), false, 1, AllowUpFileExt, ref strErrorMsg);
        if (!string.IsNullOrEmpty(strDefaultPicture) && iSelectId == 0 || iSelectId > 0)
        {
            Model.DbPicture dbPicture = new Model.DbPicture();
            dbPicture.PId = iSelectId;
            dbPicture.PTitle = strTitle;
            dbPicture.PViews = iHits;
            dbPicture.POrder = iOrder;
            dbPicture.PStatus = iStatus;
            dbPicture.PNotes = strNotes;
            dbPicture.PAddTime = DateTime.Now;
            dbPicture.PDefaultImg = strDefaultPicture;

            string strResult = string.Empty;
            if (DalEx.DbPictureEx.Insert(dbPicture, ref strResult) > 0)
            {
                FineUI.Alert.Show(strResult, FineUI.MessageBoxIcon.Information);
                sfObject.GetResetReference();
            }
            else
            {
                FineUI.Alert.Show(strResult, FineUI.MessageBoxIcon.Error);
            }
        }
        else
        {
            FineUI.Alert.Show("请选择要上传的图片", FineUI.MessageBoxIcon.Warning);
        }
    }
}
